package com.njupt.wuaiagent.demo.invoke;

/**
 * @Author: wujiaming
 * @CreateTime: 2025/5/10 15:35
 * @Description:
 * @Version: 1.0
 */

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONArray;


/**
 * http的方式调用AI
 */
public class HttpAiInvoke {

    public static void main(String[] args) {
        // 替换为你的实际API密钥
        String apiKey = TestApiKey.API_KEY;

        // 构建请求JSON
        JSONObject requestBody = new JSONObject();
        requestBody.set("model", "qwen-max");

        JSONObject input = new JSONObject();
        JSONArray messages = new JSONArray();

        JSONObject systemMessage = new JSONObject();
        systemMessage.set("role", "system");
        systemMessage.set("content", "You are a helpful assistant.");

        JSONObject userMessage = new JSONObject();
        userMessage.set("role", "user");
        userMessage.set("content", "你是谁？");

        messages.add(systemMessage);
        messages.add(userMessage);
        input.set("messages", messages);

        requestBody.set("input", input);

        JSONObject parameters = new JSONObject();
        parameters.set("result_format", "message");
        requestBody.set("parameters", parameters);

        // 发送请求
        String response = HttpRequest.post("https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation")
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .body(requestBody.toString())
                .execute()
                .body();

        System.out.println(response);
    }
}
